﻿using System;
using System.Collections.Generic;
using System.Web;
using dotnetCHARTING;
using System.Data;
using System.Drawing;

namespace Hxj.Common
{
    /// <summary>
    /// 
    /// </summary>
    public class Charting
    {
        private string _phaysicalimagepath;//图片存放路径
        private string _title; //图片标题
        private string _xtitle;//图片x座标名称
        private string _ytitle;//图片y座标名称
        private string _seriesname;//图例名称
        private int _picwidth = 400;//图片宽度
        private int _pichight = 600;//图片高度
        private SeriesType _type = SeriesType.Column;//统计图类型(柱形,线形等)
        private bool _use3d = false;//是否显示成3维图片
        private DataTable _dt;//统计图数据源
        private string _filename;//统计图片的名称(不包括后缀名)

        /**/
        /// <summary>
        /// 图片存放路径
        /// </summary>
        public string PhaysicalImagePath
        {
            set { _phaysicalimagepath = value; }
            get { return _phaysicalimagepath; }
        }
        /**/
        /// <summary>
        /// 图片标题
        /// </summary>
        public string Title
        {
            set { _title = value; }
            get { return _title; }
        }
        /**/
        /// <summary>
        /// 图片x座标名称
        /// </summary>
        public string XTitle
        {
            set { _xtitle = value; }
            get { return _xtitle; }
        }
        /**/
        /// <summary>
        /// 图片y座标名称
        /// </summary>
        public string YTitle
        {
            set { _ytitle = value; }
            get { return _ytitle; }
        }

        /**/
        /// <summary>
        /// 图例名称
        /// </summary>
        public string SeriesName
        {
            set { _seriesname = value; }
            get { return _seriesname; }
        }
        /**/
        /// <summary>
        /// 图片宽度
        /// </summary>
        public int PicWidth
        {
            set { _picwidth = value; }
            get { return _picwidth; }
        }
        /**/
        /// <summary>
        /// 图片高度
        /// </summary>
        public int PicHight
        {
            set { _pichight = value; }
            get { return _pichight; }
        }

        /// <summary>
        /// 统计图类型(柱形,线形等)
        /// </summary>
        public SeriesType Type
        {
            set { _type = value; }
            get { return _type; }
        }

        /// <summary>
        /// 是否将输出的图片显示成三维
        /// </summary>
        public bool Use3D
        {
            set { _use3d = value; }
            get { return _use3d; }
        }

        /// <summary>
        /// 对比图形数据源
        /// </summary>
        public DataTable DataSource
        {

            set { _dt = value; }
            get { return _dt; }
        }

        /// <summary>
        /// 生成统计图片的名称
        /// </summary>
        public string FileName
        {
            set { _filename = value; }
            get { return _filename; }
        }


        #region 柱形图

        /// <summary>
        /// 柱形图
        /// </summary>
        /// <param name="chart"></param>
        public void CreateColumn(dotnetCHARTING.Chart chart)
        {
            CreateColumn(chart, null, SeriesBy.Column);
        }
        /// <summary>
        /// 柱形图
        /// </summary>
        /// <param name="chart">chart控件</param>
        /// <param name="columnName">列名</param>
        /// <param name="seriesBy"></param>
        public void CreateColumn(dotnetCHARTING.Chart chart, string columnName, SeriesBy seriesBy)
        {
            //基本样式设置
            chart.Type = ChartType.Combo;
            SetDefault(chart, SeriesType.Column);

            chart.SeriesCollection.Add(getSeriesData(columnName, seriesBy));

            chart.DefaultSeries.DefaultElement.ShowValue = true;
            chart.ShadingEffect = true;
            chart.Series.DefaultElement.ShowValue = true;
        }
        #endregion

        #region 曲线图

        /// <summary>
        /// 曲线图
        /// </summary>
        /// <param name="chart"></param>
        public void CreateLine(dotnetCHARTING.Chart chart)
        {
            CreateLine(chart, null, SeriesBy.Column);
        }


        /// <summary>
        /// 曲线图
        /// </summary>
        /// <param name="chart"></param>
        /// <param name="columnName"></param>
        /// <param name="seriesBy"></param>
        /// <returns></returns>
        public void CreateLine(dotnetCHARTING.Chart chart, string columnName, SeriesBy seriesBy)
        {
            //基本样式设置
            chart.Type = ChartType.Combo;
            SetDefault(chart, SeriesType.Line);
            chart.ShowDateInTitle = true;
            chart.DefaultSeries.DefaultElement.ToolTip = chart.DefaultSeries.DefaultElement.Name;
            chart.SeriesCollection.Add(getSeriesData(columnName, seriesBy));

            chart.DefaultSeries.DefaultElement.ShowValue = true;//element值
            chart.ShadingEffect = true;
            
            chart.Series.DefaultElement.ShowValue = true;
        }
        #endregion

        #region 饼图

        /// <summary>
        /// 单饼图
        /// </summary>
        /// <param name="chart"></param>
        public void CreatePie(dotnetCHARTING.Chart chart)
        {
            CreatePie(chart, null);
        }


        /// <summary>
        /// 单饼图
        /// </summary>
        /// <returns></returns>
        public void CreatePie(dotnetCHARTING.Chart chart, string columnName)
        {
            CreatePie(chart, columnName, ChartType.Pie, SeriesBy.Column);
        }


        /// <summary>
        /// 多个饼图
        /// </summary>
        /// <param name="chart"></param>
        public void CreatePies(dotnetCHARTING.Chart chart)
        {
            CreatePies(chart, null);
        }

        /// <summary>
        /// 多个饼图
        /// </summary>
        /// <param name="chart"></param>
        /// <param name="columnName"></param>
        public void CreatePies(dotnetCHARTING.Chart chart, string columnName)
        {
            CreatePie(chart, columnName, ChartType.Pies, SeriesBy.Column);
        }

        /// <summary>
        /// 饼图
        /// </summary>
        /// <param name="chart"></param>
        /// <param name="columnName"></param>
        /// <param name="chartType"></param>
        /// <param name="seriesBy"></param>
        public void CreatePie(dotnetCHARTING.Chart chart, string columnName, ChartType chartType, SeriesBy seriesBy)
        {
            chart.Type = chartType;
            //但饼图以field为序列，多饼图以columns为序列
            SetDefault(chart, SeriesType.Line);

            chart.ShowDateInTitle = false;
            chart.ShadingEffect = true;//泛光效果
            chart.SeriesCollection.Add(getSeriesData(columnName, seriesBy));//添加序列到控件

            chart.DefaultSeries.DefaultElement.ShowValue = true;//element值
            chart.DefaultSeries.DefaultElement.SmartLabel.DynamicDisplay = true;
            chart.DefaultSeries.DefaultElement.SmartLabel.PieLabelMode = PieLabelMode.Automatic;
            chart.PieLabelMode = PieLabelMode.Outside;
            chart.Series.DefaultElement.ShowValue = true;
        }
        #endregion


        #region 内部方法
        /// <summary>
        /// 基本样式设定
        /// </summary>
        /// <param name="chart"></param>
        /// <param name="seriesType"></param>
        private void SetDefault(dotnetCHARTING.Chart chart, SeriesType seriesType)
        {
            chart.Title = this.Title;

            chart.Width = this.PicWidth;
            chart.Height = this.PicHight;

            if (!string.IsNullOrEmpty(this.FileName))
                chart.FileName = this.FileName;

            chart.DefaultSeries.Type = seriesType;
            //标题的颜色
            chart.TitleBox.Label.Color = Color.Blue;
            //标题的背景色
            chart.TitleBox.Background.Color = Color.White;
            //是否显示底部工具栏
            chart.Mentor = true;
            //图片存放的位置
            chart.TempDirectory = this.PhaysicalImagePath;
            //说明总数的文字背景
            chart.LegendBox.Background.Color = Color.Pink;
            //默认柱样式
            chart.ShadingEffectMode = dotnetCHARTING.ShadingEffectMode.Five;
            //显示元素值
            chart.DefaultElement.ShowValue = true;
            //chart.DefaultElement.ToolTip = chart.DefaultElement.Name;
            //chart.ShowDateInTitle = false;
            //chart.Series.Element.SmartLabel.Text = chart.DefaultElement.Name;
            //控件是否是3D格式
            chart.Use3D = this.Use3D;
            //阴影效果
            chart.ShadingEffect = true;

            //Y轴说明
            chart.YAxis.Name = this.YTitle;
            //dotnetCHARTING.ScaleRange range = new dotnetCHARTING.ScaleRange(1, 200); //Y 轴的刻度范围
            //chart.YAxis.Label.Color = Color.Red;
            //chart.YAxis.ScaleRange = range;
            //chart.YAxis.Scale = dotnetCHARTING.Scale.Range;
            chart.YAxis.Label.Font = new Font("Arial Black", 9, FontStyle.Bold);
            //刻度
            chart.XAxis.Name = this.XTitle;
            chart.YAxis.TickLabelPadding = 0;
            chart.YAxis.Line.StartCap = System.Drawing.Drawing2D.LineCap.Square;
            chart.YAxis.Line.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            chart.YAxis.Line.Width = 5;//左侧label
            chart.YAxis.Line.Color = Color.Gray;

            //X轴说明
            chart.XAxis.Label.Font = new Font("Arial Black", 9, FontStyle.Bold);
            chart.XAxis.Line.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            chart.XAxis.Line.Width = 2;
            chart.XAxis.StaticColumnWidth = 80; //每个单元格的宽度
            chart.XAxis.SweepAngle = 45;
            //chart.XAxis.Line.Color = Color.Gray;
            chart.XAxis.MinorInterval = 5;
            chart.DefaultBox.Visible = true;

            //显示值格式化
            chart.YAxis.NumberPercision = 2;
            //chart.Series.LegendEntry.Value
            //chart.YAxis.FormatString = "{n:0}";
            //chart.LegendBox.e
        }

        /// <summary>
        /// 单序列
        /// </summary>
        /// <returns></returns>
        private SeriesCollection getSeriesData()
        {
            SeriesCollection SC = new SeriesCollection();
            DataTable dt = this._dt;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Series s = new Series();
                s.Name = dt.Rows[i][0].ToString();
                Element e = new Element();

                // 每元素的名称
                e.Name = dt.Rows[i][0].ToString();
                // 每元素的大小数值
                e.YValue = Convert.ToDouble(dt.Rows[i][1].ToString());

                s.Elements.Add(e);
                SC.Add(s);
            }
            return SC;
        }
        /// <summary>
        /// 多序列元素名字段（例如：指标名）
        /// </summary>
        /// <param name="columnName"></param>
        /// <returns></returns>
        private SeriesCollection getSeriesData(string columnName, SeriesBy seriesBy)
        {
            if (!string.IsNullOrEmpty(columnName))
            {
                //--多序列测试
                //序列集合（年份序列）
                SeriesCollection SC = new SeriesCollection();
                //x轴名字集合
                System.Collections.Generic.List<string> xNames = new System.Collections.Generic.List<string>();
                if (seriesBy == SeriesBy.Column)
                {
                    //序列名
                    for (int i = 1; i < DataSource.Columns.Count; i++)
                    {
                        xNames.Add(DataSource.Columns[i].ColumnName);
                    }
                    for (int k = 0; k < xNames.Count; k++)
                    {
                        Series series = new Series(xNames[k]);
                        for (int j = 0; j < DataSource.Rows.Count; j++)
                        {
                            Element e = new Element();
                            e.Name = DataSource.Rows[j][columnName].ToString();
                            e.YValue = Convert.ToDouble(DataSource.Rows[j][k + 1]);
                            series.Elements.Add(e);
                        }
                        SC.Add(series);
                    }
                }
                else if (seriesBy == SeriesBy.Field)
                {
                    //序列名
                    for (int i = 1; i < DataSource.Columns.Count; i++)
                    {
                        xNames.Add(DataSource.Columns[i].ColumnName);
                    }
                    for (int j = 0; j < DataSource.Rows.Count; j++)
                    {
                        Series series = new Series(DataSource.Rows[j][columnName].ToString());
                        for (int k = 0; k < xNames.Count; k++)
                        {
                            Element e = new Element();
                            e.Name = xNames[k];
                            e.YValue = Convert.ToDouble(DataSource.Rows[j][k + 1]);
                            series.Elements.Add(e);
                        }
                        SC.Add(series);
                    }
                }

                return SC;
            }
            else
            {
                return getSeriesData();
            }
        }
        #endregion

        public enum SeriesBy
        {
            Column, Field
        }
    }
}

